package com.wmyg.scanninggun.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.wmyg.scanninggun.bean.*;
import com.wmyg.scanninggun.sdk.ManageBuilder;
import com.wmyg.scanninggun.sdk.SmartManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Slf4j
@Repository
public class FabricDao {

    @Autowired
    SmartManager smartManager;

    public List<String> getPreData(String batchkey) throws Throwable {
//        ArrayList<DataPre> dataPres = new ArrayList<>();
        String[] args = new String[]{batchkey};
        log.info("Begin to invoke {}", batchkey);
//        SmartManager smartManager = ManageBuilder.buildManagerToUse("south", "User1", "antifake", "developchannel");
        log.info("SmartManager Build Success");
        Map<String, String> map = smartManager.query("dataPrepare", args);
        log.info("Success get data:" + map.get("data"));
        List<String> keys = JSONArray.parseArray(map.get("data"), String.class);
//        keys.forEach(k -> {
//            try {
//                log.info("Begin to invoke {}", k);
//                SmartManager s = ManageBuilder.buildManagerToUse("south", "User1", "antifake", "developchannel");
//                log.info("SmartManager Build Success");
//                Map<String, String> m = s.query("queryCar", new String[]{k});
////                log.info("Success get data:" + m.get("data"));
//                DataPre dataPre = new DataPre();
//                dataPre.setKey(k);
//                dataPre.setValue(m.get("data"));
//                dataPres.add(dataPre);
//            } catch (Throwable throwable) {
//                throwable.printStackTrace();
//            }
//        });
        return keys;
    }

    public List<String> getPreDataForEach(String key) throws Throwable {
        String[] args = new String[]{key};
        log.info("Begin to invoke {}", key);
//        SmartManager smartManager = ManageBuilder.buildManagerToUse("south", "User1", "antifake", "developchannel");
        log.info("SmartManager Build Success");
        Map<String, String> map = smartManager.query("dataPrepareForEach", args);
        log.info("Success get data:" + map.get("data"));
        List<String> qrCodes = JSONArray.parseArray(map.get("data"), String.class);
        return qrCodes;
    }

    public void uploadBill(Bill bill) throws Throwable {
        String boxes = JSON.toJSONString(bill.getBoxes());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String creatimeString = sdf.format(bill.getCreatime());
        String[] args = new String[]{bill.getCaseId(), boxes, bill.getDeviceId(), Integer.toString(bill.getOperator()), creatimeString};
//        SmartManager smartManager = ManageBuilder.buildManagerToUse("south", "User1", "antifake", "developchannel");
        Map<String, String> map = smartManager.invoke("enterCaseBill", args);
        if ("success".equals(map.get("code"))) {
            log.info("Upload case " + bill.getCaseId() + " of " + bill.getBillId() + " success");
        }
    }

}
